<?php
/* @var $this BuyController */

$this->breadcrumbs=array(
	'Giỏ hàng',
);
$carts = array();
if(isset($_SESSION['cart'])){
   $carts = $_SESSION['cart'];
}

?>
<div class="shop-cart">
<h3 class="title">Giỏ hàng</h3>
<table cellspacing="1" cellpadding="5" class="table-one-cart" border="1">
   <tr class="bg-header">
      <th width="50">STT</th>
      <th>Tên sản phẩm</th>
      <th width="80">Số lượng</th>
      <th width="110">Giá (VNĐ)</th>
      <th width="110">Số tiền (VNĐ)</th>
      <th width="50">&nbsp;</th>
   </tr>
   <tr>
      <?php if(count($carts) == 0){?>
         <td colspan="6" align="center" style="padding:20px">Giỏ hàng của bạn không có sản phẩm nào</td>
      <?php }else{
         $i = 0;  
         $total_money = 0;
         $book = new Books(); 
         foreach($carts as $key => $value){
            $i++;
            $info = $book->getInfo($key,array('book_name','book_image','book_price'));
            $name = $info['book_name'];
            $image = Yii::app()->request->baseUrl.'/uploads/pictures/product/thumb_'.$info['book_image'];
            $qty = $value;
            $price = $info['book_price'];
            $total_money += ($qty*$price);
            ?>
            <tr>
               <td class="text-center"><?php echo $i?></td>
               <td class="text-center"><img src="<?php echo $image?>" /><br /><?php echo $name?></td>
               <td class="text-center"><input onkeyup="updateCart(<?php echo $key?>,this)" type="text" value="<?php echo $qty?>" /></td>
               <td class="price text-right"><?php echo Str::formatNumber($price) ?></td>
               <td class="price text-right"><?php echo Str::formatNumber($qty*$price)?></td>
               <td><span class="btn btn-danger"  onclick="removeCart(<?php echo $key?>)">Xóa</span></td>
            </tr>
            <?php
         }?>
         <tr>
            <td class="text-right" colspan="4">Tổng tất cả</td>
            <td class="price" colspan="4"><?php echo Str::formatNumber($total_money)?></td>
         </tr>
      <?php }?>
      
   </tr>
</table>
<?php if(count($carts) >= 0){?>
<div class="customer form">
   <h3 class="title">Thông tin khách hàng</h3>
   <?php if(Yii::app()->user->hasFlash('success')){
      echo CHtml::tag('div',array(
         'class' => 'bg-success well-lg',
      ),Yii::app()->user->getFlash('success'));
   }?>
   <?php if(Yii::app()->user->hasFlash('error')){
      echo CHtml::tag('div',array(
         'class' => 'has-error well-lg',
      ),Yii::app()->user->getFlash('error'));
   }?>
   <?php if(Yii::app()->session['user_id'] != null){
      $user = Users::model()->findByPk(Yii::app()->session['user_id']);
      
      $orders->attributes = array(
         'ord_name_customer' => $user->use_fullname,
         'ord_add_customer' => $user->use_address,
         'ord_phone_customer' => $user->use_phone,
      );
   }?>
    <?php $form = $this->beginWidget('CActiveForm',array(
      'id' => 'shipping-form',
      'method' => 'POST',
   ));?>
   <form method="POST" action="/buy/orders">
      <p class="form-row">
         <?php echo $form->labelEx($orders,'ord_name_customer');?>
         <?php echo $form->telField($orders,'ord_name_customer');?>
      </p>
      <p class="form-row">
         <?php echo $form->labelEx($orders,'ord_add_customer');?>
         <?php echo $form->telField($orders,'ord_add_customer',array(
            'style' => 'width:400px',
         ));?>
      </p>
      <p class="form-row">
         <?php echo $form->labelEx($orders,'ord_phone_customer');?>
         <?php echo $form->telField($orders,'ord_phone_customer');?>
      </p>
      <input class="btn btn-success" type="submit" value="Đặt hàng"/>
      <input class="btn btn-danger" type="button" value="Hủy" onclick="window.history.go(-1)"/>
   <?php $this->endWidget();?>
</div>
<?php }?>
</div>